package com.example.demo.test.netty;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

import java.util.Date;

/**
 * @auth
 **/
public class Client {
    public static void main(String [] args){
        Bootstrap bootstrap = new Bootstrap();
        NioEventLoopGroup group = new NioEventLoopGroup();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) {
                        ch.pipeline().addLast(new StringDecoder());
                    }
                });
        Channel channel = bootstrap.connect("127.0.0.1",8899).channel();
        System.out.println(channel.isRegistered());
        int i= 0;
        while (true) {
            System.out.println(channel.isRegistered());
            System.out.println(channel.isActive());
            channel.writeAndFlush(new Date() + ": hello world!");
            System.out.println("发送---->"+(i++));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
